//包含头文件-----------------------------------------------------------------------------

#include <REGX52.H>
#include "Nixie.h"
#include "Timer1.h"
#include "MatrixKeyT.h"
#include "Ultrasonic.h"

//定义全局变量--------------------------------------------------------------------------
unsigned char KeyNum;


typedef struct
{
	int Distance;
	int Key8_Set_Distance;	//S8按键设置的参数
	
	
}Param_InitTypeDef;


typedef struct
{
	unsigned char Read_chaoshengbo;
	unsigned char Show_Mode;
	unsigned char LED_Turn;
	
	
}Flag_InitTypeDef;




 Param_InitTypeDef 	Param;
 Flag_InitTypeDef 	Flag;




void LED_OFF(void)
{
	P1_3 = 1;
	P1_4 = 1;
	P1_5 = 1;
	P1_6 = 1;
	P1_7 = 1;
}


/*显示距离函数*/
void Show_Distance(void)
{
	Nixie(1, 11);	//显示U1
	Nixie(2, 1);
	
	Nixie (6, (unsigned char)(Param.Distance / 100 ) );
	Nixie (7, (unsigned char)(Param.Distance / 10 % 10 ) );
	Nixie (8, (unsigned char)(Param.Distance % 10 ) );
}
/*显示参数函数*/
void Show_Daram(void)
{
	Nixie(1, 11);	//显示U2
	Nixie(2, 2);
	
	Nixie ( 6, (unsigned char)(Param.Key8_Set_Distance / 100) );	//数位分离，显示参数
	Nixie ( 7, (unsigned char)(Param.Key8_Set_Distance / 10 % 10) );
	Nixie ( 8, (unsigned char)(Param.Key8_Set_Distance % 10) );
}


//主程序------------------------------------------------------------------------------------------------------
void main()
{
	Timer1_Init();
	Ultrasonic_Init();	//超声波模块初始化
	Param.Key8_Set_Distance = 30;	//上电初始化为30
	
	while(1)
	{
		
		KeyNum = Key();	//读取按键
		
		
		/*超声波读取********************************************************************************************/
		if(Flag.Read_chaoshengbo == 1)	//如果到了读取超声波的时间
		{
			Param.Distance = Ultrasonic();	//赋值超声波测到的距离
			Flag.Read_chaoshengbo = 0;
		}
		
		
		LED_OFF();	//指示灯L4-L8处于常熄灭状态。
		
		
		
		/*按键部分********************************************************************************************/
		if (4 == KeyNum)	//如果按键4按下，能够切换“距离显示界面”、“参数显示界面”。
		{
			Flag.Show_Mode = !Flag.Show_Mode ;	//显示模式的标志位加加
		}
		else if (8 == KeyNum)	//如果按键4按下
		{
			if(Flag.Show_Mode == 0)	//并且是显示界面模式
			{
				Param.Key8_Set_Distance = Param.Distance;	//将当前距离 设置为距离参数
			}
		}
		
		/*界面显示**********************************************************************************************/
		if (Flag.Show_Mode == 0)	
		{
			Show_Distance();	//显示模式0  显示距离
			
			P1_0 = 0;
			P1_1 = 1;	//L1指示灯点亮，L2灭
		}
		else if (Flag.Show_Mode == 1)
		{
			Show_Daram();		//显示模式1  显示 参数界面
			
			P1_0 = 1;
			P1_1 = 0;	//L1指示灯点亮，L2灭
			
			if (12 == KeyNum)	//如果在参数界面，并且按下按键12
			{
				Param.Key8_Set_Distance += 10;	//距离+10
			}
			if (16 == KeyNum)	//如果在参数界面，并且按下按键12
			{
				Param.Key8_Set_Distance -= 10;	//距离+10
			}
			if (Param.Key8_Set_Distance <= 0)
			{
				Param.Key8_Set_Distance = 0;
			}
			
		}
		
		if (Param.Distance > Param.Key8_Set_Distance)
		{
			if (Flag.LED_Turn == 1)
				P1_2 = 0;	//点亮L3
		}
		
		
		
		
		
		
	}
}





//定时器1的中断函数---------------------------------------------------------------------
void Timer1_Routine() interrupt 3  
{               //1毫秒@12.000MHz
	static unsigned int LEDCount, anjian, SMGCount, Ultrasonic_Read ;

	TL1 = 0x18;		
	TH1 = 0xFC;		
	
	LEDCount ++;
	anjian ++;
	SMGCount ++;
	Ultrasonic_Read ++;
	
	if(LEDCount >= 200)	//200ms的LEd闪烁
	{
		Flag.LED_Turn = ! Flag.LED_Turn;	//200ms反转一次
		LEDCount = 0;
	}
	
//	if(SMGCount >= 1)	//1ms刷新数码管
//	{
//		Nixie_Loop();
//		SMGCount = 0;
//	}
	
	if(anjian >= 100)	//100ms扫描一次矩阵按键
	{
		MatrixKey_Loop();
		anjian = 0;	//软件清零标志位
	}
	
	if(Ultrasonic_Read >= 5000 )
	{
		Flag.Read_chaoshengbo = 1;
		Ultrasonic_Read = 0;
		
	}		
}




